/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package src.report.loan;

import java.io.IOException;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import net.sf.jasperreports.engine.JRDataSource;
import net.sf.jasperreports.engine.JREmptyDataSource;
import net.sf.jasperreports.engine.JRException;
import src.report.AbstractBaseReportBean;
import src.report.ReportFilter;

/**
 *
 * @author Yusup Jauhari Shandi <in.visi>
 */
@ManagedBean
@SessionScoped

public class KwitansiTagihanBean extends AbstractBaseReportBean{
    private final String COMPILE_FILE_NAME = "loan/kwitansiTagihan";
    
 
            
    @Override
    protected JRDataSource getJRDataSource() {
      
        return new JREmptyDataSource();
    }

    @Override
    protected String getCompileFileName() {
        return COMPILE_FILE_NAME;
    }
    
    public String execute(ReportFilter reportFilter) {
	try {
        /*    super.getReportParameters().put("p_jangka_waktu", this.period);
            super.getReportParameters().put("p_metod", this.metod);
            super.getReportParameters().put("p_product_name", this.productName);
            super.getReportParameters().put("p_principal_amount", this.principalAmount);
            super.getReportParameters().put("p_interest_rate", this.interestRate);
            * 
            * li.paid= 0 and extract(MONTH from li.due_date)=5
and extract(YEAR from li.due_date) = 2012
and l.branch_id = 2
            */
            StringBuilder where = new StringBuilder("");
            if (reportFilter.getBranchId()!=null)
                where.append(" and l.branch_id = ").append(reportFilter.getBranchId());
            if (reportFilter.getPayPoint()!=null)
                where.append(" and l.paypoint_id = ").append(reportFilter.getPayPoint());
            if (reportFilter.getMonth()!=null){
                reportFilter.setMonth(reportFilter.getMonth()+1);
                if (reportFilter.getMonth()>12){
                    reportFilter.setMonth(1);
                    reportFilter.setYear(reportFilter.getYear()+1);
                }
                where.append(" and extract(MONTH from li.due_date) = ").append(reportFilter.getMonth());
            }
            if (reportFilter.getYear()!=null)
                where.append(" and extract(YEAR from li.due_date) = ").append(reportFilter.getYear());
            if (reportFilter.getProductId()!=null)
                where.append(" and l.loanproduct_id = ").append(reportFilter.getProductId());
            if (where.length()>0){
                where.replace(0, 5, "");                
                where.insert(0, " where ");
            }
            super.getReportParameters().put("p_where", where.toString() );
            
            super.prepareReportViaConn();
         //   super.prepareReport();
	} catch (JRException | IOException e) {}
	
        return null;
    }

  
    
}
